﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using FANUC.DTO;
using Newtonsoft.Json;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class UnitHistoryInquireController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        /// <summary>
        /// GET: api/ApiPartUnitHistory/Part_Serial
        /// Get part unit process history data by part_serial
        /// </summary>
        /// <param name="id">Part_Serial</param>
        /// <returns>PartUnitHistoryDtoOutput</returns>
        [ResponseType(typeof(PartUnitHistoryDtoOutput))]
        public IHttpActionResult Get(string id)
        {
            var partHistoryRecords = db.Part_Unit.Where(a => a.Part_Serial == id).ToList();
            if (partHistoryRecords.Count <= 0)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Part_Unit, PartUnitHistoryRecordDto>().ForMember(dst => dst.Robot_YH_No, src => src.MapFrom(s => s.Robot.YH_No)).ForMember(dst => dst.Part_State, src => src.MapFrom(s => s.Part_State.State)));
            List<PartUnitHistoryRecordDto> recordsDto = new List<PartUnitHistoryRecordDto>();
            recordsDto = AutoMapper.Mapper.Map<List<PartUnitHistoryRecordDto>>(partHistoryRecords);
            PartUnitHistoryDtoOutput data = new PartUnitHistoryDtoOutput();
            data.Part_Serial = id;
            data.PartUnitHistoryRecords = recordsDto;

            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        {
            return null;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
